<?php

/**
 * This is the model class for table "{{history}}".
 *
 * The followings are the available columns in table '{{history}}':
 * @property integer $id
 * @property integer $object_id
 * @property string $field_name
 * @property string $old_value
 * @property string $new_value
 * @property string $module
 * @property string $action
 * @property string $updated_by
 * @property string $update_date
 */
class History extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return History the static model class
	 */
	
	public $keyword;
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{history}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('object_id, field_name', 'required'),
			array('object_id', 'numerical', 'integerOnly'=>true),
			array('field_name, module, action, updated_by', 'length', 'max'=>50),
			array('old_value, new_value', 'length', 'max'=>300),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('keyword, id, object_id, field_name, old_value, new_value, module, action, updated_by, update_date', 'safe', 'on'=>'search'),
			array('keyword, id, object_id, field_name, old_value, new_value, module, action, updated_by, update_date', 'safe', 'on'=>'history'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => t('LBL_ID','history'),
			'object_id' => t('LBL_OBJECT_ID','history'),
			'field_name' => t('LBL_FIELD_NAME','history'),
			'old_value' => t('LBL_OLD_VALUE','history'),
			'new_value' => t('LBL_NEW_VALUE','history'),
			'module' => t('LBL_MODULE','history'),
			'action' => t('LBL_ACTION','history'),
			'updated_by' => t('LBL_UPDATED_BY','history'),
			'update_date' => t('LBL_UPDATE_DATE','history'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$this->keyword = trim($this->keyword);
		$criteria->addSearchCondition('field_name', $this->keyword, true);
		$criteria->compare('id',$this->id);
		$criteria->compare('object_id',$this->object_id);
		$criteria->compare('field_name',$this->field_name,true);
		$criteria->compare('old_value',$this->old_value,true);
		$criteria->compare('new_value',$this->new_value,true);
		$criteria->compare('module',$this->module,true);
		$criteria->compare('action',$this->action,true);
		$criteria->compare('updated_by',$this->updated_by,true);
		$criteria->compare('update_date',$this->update_date,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function getHistoryByProfileId($id){
		$criteria=new CDbCriteria;
		$this->keyword = trim($this->keyword);
		$criteria->addSearchCondition('field_name', $this->keyword, true);
		$criteria->compare('object_id',$id);
			
		$sort = new CSort ();
		$sort->defaultOrder = array (
				'update_date' => CSort::SORT_DESC
		);
			
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
				'pagination' => array (
						'pageSize' => LGHelpers::getPageSize ( $this->count () )
				),
				'sort' => $sort
		));
	}
	
	public function beforeSave() {
		if (parent::beforeSave ()) {
			// Get user is logging
			$logedInUser = User::model ()->findByPk ( Yii::app ()->user->id );
			$this->updated_by = $logedInUser->username;	
			return true;
		} else {
			return false;
		}
	}
}